import java.util.concurrent.Semaphore;

public class Demo30 {
    public static void main(String[] args) {
        Semaphore semaphore = new Semaphore(4);
        Runnable runnable = () -> {
            try {
                System.out.println("申请资源");
                semaphore.acquire();
                System.out.println("已获取资源");
                Thread.sleep(1000);
                semaphore.release();
                System.out.println("已释放资源");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        };

        for (int i = 0 ; i < 20; ++i) {
            Thread thread = new Thread(runnable);
            thread.start();
        }
    }
}
